package com.poul.acfun.ac_view.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.poul.acfun.Constants;
import com.poul.acfun.R;
import com.poul.acfun.ac_model.bean.DaoMaster;
import com.poul.acfun.ac_model.bean.DaoSession;
import com.poul.acfun.ac_model.bean.PushBean;
import com.poul.acfun.ac_model.bean.PushEntry;
import com.poul.acfun.ac_model.bean.PushEntryDao;
import com.poul.acfun.ac_model.bean.WelcomeBean;
import com.poul.acfun.ac_utils.AcNetUtil;
import com.poul.acfun.ac_utils.DateUtil;
import com.poul.acfun.ac_utils.FileUtil;
import com.poul.acfun.listener.OnGetListener;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends Activity {

    SimpleDraweeView welcomeImg;
    private String imgUrl;
    private MyHandler myHandler;
    private Gson gson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_welcome);
        welcomeImg = (SimpleDraweeView)findViewById(R.id.welcome_img);

        myHandler = new MyHandler(this);

        SharedPreferences sharedPreferences = getSharedPreferences(Constants.PREFERENCES_NAME_WLCOME, MODE_PRIVATE);
        imgUrl = sharedPreferences.getString(Constants.KEY_WLCOME_IMG_URL, "");
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), Constants.WELCOME_FILE_NAME);

        if (file.exists() && !DateUtil.isExpried(file.lastModified())) {
            loadLocalImag();
        } else {
            requestWelcomeImage();
        }

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                myHandler.sendMessage(Message.obtain());
            }
        }, 3000);

    }

    private void loadLocalImag() {

    }

    private void requestWelcomeImage() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "push.db");
        DaoMaster master = new DaoMaster(helper.getWritableDb());
        DaoSession session = master.newSession();
        PushEntryDao pushEntryDao = session.getPushEntryDao();
        gson = new Gson();
        List<PushEntry> been = pushEntryDao.loadAll();
        AcNetUtil.getWelcomImage(new OnGetListener() {
            @Override
            public void onNext(Object object) {
                final WelcomeBean welcomeBean = (WelcomeBean) object;
                PushBean pushBean = (PushBean) object;
                PushEntry bean = new PushEntry();
                bean.setJsonString(gson.toJson(pushBean));
                Date date = new Date();
                bean.setDate(date.getTime());
                pushEntryDao.insert(bean);
//                welcomeImg.setImageURI();
            }
        });
    }

    private static class MyHandler extends Handler {
        private final WeakReference<WelcomeActivity> mActivity;

        public MyHandler(WelcomeActivity activity) {
            mActivity = new WeakReference<WelcomeActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            WelcomeActivity activity = mActivity.get();
            if (activity != null) {
                Intent intent = new Intent(activity, MainActivity.class);
                activity.startActivity(intent);
                activity.finish();
            }
        }
    }
}
